$(function(){
//	submitForm(1,10);
	bindBall();
	$(".selectLine a").click(function(){
		resultAjax(getJsPath()+"../comm/taskSegment.json","lineId="+$(this).attr("id"),setTaskSegment,"JSON");
		getSendBall($(this).attr("id"));
	});
	$(".exportExlBtn").click(function(){
		$('.zy-i-input').css({border:"none"});
		$('.zy-i-input').css({height:"35px"});
		var lineName=window.line.val('text');
		var taskName=window.task.val('text');
		var name = lineName+taskName+$(this).attr("exportName");


		resultAjax(path+"/comm/checkOutTime.json","",function(data,name){
			var exportFile = $(this).EXPT({
				name:name ,
				yesBtnAct: function(callback){
					exportPDF(exportFile.name);
					callback();
				}
			});

		},"json",name);

	});
	$(".find").click(function(){
		searchData();
		window.isDownPDF=true;
	});
	$(".rep-form img").each(function(){
		$(this).parent().css("text-align","center");
		$(this).parent().attr("img",path+"/static/img/chart_no_data.png");
		$(this).attr("src",path+"/static/img/chart_no_data.png")
	});
});

function exportPDF(fileName)
{
	if(window.isDownPDF)
	{
		var lineName=window.line.val('text');
		var taskName=window.task.val('text');
		var args=[];
		$(".defect-down").each(function(){
			args.push(this);
		});

		var obj = [window.line,window.task];
		if(checkForm(obj)&&checkFormVerify($("#JQ_form"))) {
			downPDF(args,lineName+taskName+"管道壁厚统计图",resetData,fileName);
		}

	}
}

function resetData()
{
	findResult(window.prevData);
}

function searchData()
{
	var lineIdVal=window.line.val();
	var taskIdVal=window.task.val();
	var beginMileageVal=window.begin.val('mile');
	var endMileageVal=window.end.val('mile');


	var obj = [window.line,window.task,window.begin,window.end];

	if(checkForm(obj)&&checkFormVerify($("#JQ_form")))
	{
		var data="lineId="+lineIdVal+"&taskId="+taskIdVal;
		data+="&beginMileage="+beginMileageVal+"&endMileage="+endMileageVal;
		resultAjax(getJsPath()+"../insp/statistics/wall/search.json",data,findResult,"JSON");
	}

}
/**
 * 
 * 查询数据返回
 * @param data
 */
function findResult(data)
{
	window.prevData=data;
	//其他统计饼状图数据
	var pieData= [];
	var pieName= [];
	var countNum=0;
	//列表
	$("#dataTab tr").each(function(index){
		if(index>0)
		{

			$(this).remove();
		}
	});
	for(var i=0;i<data.list.length;i++)
	{
		$("#dataTab").append('<tr><td class="first">'+'壁厚长度'+data.list[i].BH+'（mm）</td><td>'+data.list[i].NUM+'</td></tr>');
		countNum+=parseFloat(data.list[i].NUM);
	}
	
	$("#dataTab td:eq(1)").html(countNum.toFixed(3));
	
	for(var i=0;i<data.pie.length;i++)
	{
		pieName.push(data.pie[i].BH);
		pieData.push({name:data.pie[i].BH,value:parseInt(data.pie[i].LENGTH)})
	}

	if(pieData.length > 0)
	{

		var picECharts = new PicEChart();
		picECharts.addHollowPieByML({legends:pieName,centerX:"25%",legendY:"15%",data:pieData,divObject:'pie',legendX:"60%"});
		$("#pie").parent().find("i").remove();
	}else{
		$("#pie").html("<img src='"+$("#pie").attr("img")+"'/>");
		//$("#pie").parent().append("<i></i>");
	}
	
	
	
	var line=[];
	var cx=1;
	if(getMaxNum(data.line) > 10000)
	{
		cx =1000;
	}
	console.log(cx);
	for(var i=0;i<data.line.length;i++)
	{
		//console.log(data.line[i].TYPE);
		line.push([getDataVal(data.line[i].BH),parseFloat((parseFloat(data.line[i].TYPE)/cx).toFixed(1))]);
		//console.log(data.line[i].BH);
	}
	if(line.length > 0)
	{
		new createLineGraph("#line",line,cx == 1000?"(km)":"(m)");
		$("#line").parent().find("i").remove();
	}else{
		$("#line").html("<img src='"+$("#line").attr("img")+"'  />");
		//$("#line").parent().append("<i></i>");
	}
	
}

getDataVal=function(val)
{
		if(val == "-")
		{
			return "-";
		}
	return parseFloat(val);
}



getMaxNum=function(data)
{
	var max = 0;

	for(var d in data)
	{
		if(max < (parseFloat(data[d].TYPE)))
		{
			max = (parseFloat(data[d].TYPE));
		}
	}
	return max;
};
